<?php


namespace app\admin\controller;


use think\Db;

class MessageLeft
{
        public function select_message_left(){
            $fromid=$_POST['fromid'];
            $result_message=Db::table("message_left")->where("fromid",$fromid)->order('id desc')->select();
            echo json_encode($result_message,JSON_UNESCAPED_UNICODE);
        }

        public function update_message_left(){
            $fromid=$_POST['fromid'];
            $fromName=$_POST['fromName'];
            $toid=$_POST['toid'];
            $toName=$_POST['toName'];
            $last_message=$_POST['last_message'];
            $time=$_POST['time'];

            //查询有没有fromid对toid 和 toid对fromid的记录

            //没有则返回空数组 查询我对他的记录
            $res_my_to_your=Db::table("message_left")->where("fromid",$fromid)->where("toid",$toid)->select();
            if(sizeof($res_my_to_your)){
                //有数据则删除这条数据重新插入一条数据
                    for($i=0;$i<sizeof($res_my_to_your);$i++){
                        Db::table('message_left')->delete($res_my_to_your[$i]['id']);
                    }

                    //重新插入一条数据
                    $data_row=["fromid"=>$fromid,"fromName"=>$fromName,"toid"=>$toid,"toName"=>$toName,"last_message"=>$last_message,"time"=>$time];
                    Db::table("message_left")->insert($data_row);


            }else{
                    //没有数据则直接插入一条数据
                    $data_row=["fromid"=>$fromid,"fromName"=>$fromName,"toid"=>$toid,"toName"=>$toName,"last_message"=>$last_message,"time"=>$time];
                    Db::table("message_left")->insert($data_row);


            }


            //没有则返回空数组 查询他对我的记录
            $res_your_to_my=Db::table("message_left")->where("fromid",$toid)->where("toid",$fromid)->select();
            if(sizeof($res_your_to_my)){

                //有数据则删除这条数据重新插入一条数据
                    for($i=0;$i<sizeof($res_your_to_my);$i++){
                        Db::table('message_left')->delete($res_your_to_my[$i]['id']);
                    }

                    //重新插入一条数据
                    $data_row=["fromid"=>$toid,"fromName"=>$toName,"toid"=>$fromid,"toName"=>$fromName,"last_message"=>$last_message,"time"=>$time];
                    Db::table("message_left")->insert($data_row);


            }else{

                    //没有数据则直接插入一条数据
                    $data_row=["fromid"=>$toid,"fromName"=>$toName,"toid"=>$fromid,"toName"=>$fromName,"last_message"=>$last_message,"time"=>$time];
                    Db::table("message_left")->insert($data_row);


            }

            //返回最新的message列表
            $result_message=Db::table("message_left")->where("fromid",$fromid)->order('id desc')->select();
            echo json_encode($result_message,JSON_UNESCAPED_UNICODE);

        }

        public function delete_message_left(){

            //删除双方的messageLift列表
            $fromid=$_POST['fromid'];
            $toid=$_POST['toid'];

            //删除双方的messageLift列表
            Db::table("message_left")->where("fromid",$fromid)->where("toid",$toid)->delete();
            Db::table("message_left")->where("fromid",$toid)->where("toid",$fromid)->delete();

            $result_message=Db::table("message_left")->where("fromid",$fromid)->order('id desc')->select();
            echo json_encode($result_message,JSON_UNESCAPED_UNICODE);
        }
}